package com.example.attrimirror.data;

import android.content.Context;
import android.widget.Toast;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.example.attrimirror.data.model.Attribute;

public class DataInitializer {
    private final Context context;
    private final Gson gson;

    private final CheckinInitializer checkinInitializer;
    private final CollectionInitializer collectionInitializer;

    public DataInitializer(Context context) {
        this.context = context;
        this.gson = new GsonBuilder().setPrettyPrinting().create();
        this.checkinInitializer = new CheckinInitializer();
        this.collectionInitializer = new CollectionInitializer();
    }

    // 初始化签到数据文件
    public void initCheckinFile(File dataDir) {
        checkinInitializer.initialize(context, dataDir);
    }

    // 初始化仓库数据文件
    public void initCollectionFile(File dataDir) {
        collectionInitializer.initialize(context, dataDir);
    }

    // 初始化属性数据文件
    public void initAttributeFile(File dataDir) {
        File attributeFile = new File(dataDir, "attribute.json");
        try {
            if (!attributeFile.exists()) {
                if (!attributeFile.createNewFile()) {
                    Toast.makeText(context, "创建属性数据文件失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                try (FileWriter writer = new FileWriter(attributeFile)) {
                    List<Attribute> attributes = new ArrayList<>();
                    
                    // 创建正面属性
                    attributes.add(createAttribute(
                        "attribute.action", "行动", 
                        "主动、高效完成任务，独立解决问题", true));
                        
                    attributes.add(createAttribute(
                        "attribute.study", "进修", 
                        "参与备考/培训/工作等成就和进展", true));
                        
                    attributes.add(createAttribute(
                        "attribute.growth", "成长", 
                        "学习新技能，独立思考和复盘", true));
                        
                    attributes.add(createAttribute(
                        "attribute.order", "秩序", 
                        "内心安宁，处事平和，有规划", true));
                        
                    attributes.add(createAttribute(
                        "attribute.health", "健康", 
                        "饮食营养，作息规律，适度运动", true));
                        
                    attributes.add(createAttribute(
                        "attribute.beauty", "美丽", 
                        "面部、表情、仪态、声音、气质、身材", true));
                        
                    attributes.add(createAttribute(
                        "attribute.family", "家人", 
                        "亲人之间互帮互助", true));
                        
                    attributes.add(createAttribute(
                        "attribute.finance", "理财", 
                        "把钱用在刀刃上，收支平衡，财源滚滚", true));
                        
                    attributes.add(createAttribute(
                        "attribute.create", "创造", 
                        "写功能、发帖子、做手工等从无到有的过程", true));
                        
                    attributes.add(createAttribute(
                        "attribute.communicate", "沟通", 
                        "获得好感，维持关系，不无谓争辩", true));
                        
                    attributes.add(createAttribute(
                        "attribute.relation", "关系", 
                        "扩大和深化交际，不得罪、不争辩", true));

                    attributes.add(createAttribute(
                        "attribute.deed", "善行", 
                        "帮助他人、坚守正义、体现共享和贡献", true));

                    // 创建负面属性
                    attributes.add(createAttribute(
                        "attribute.lazy", "怠惰",
                        "无所事事，对于认定的事情没有全力以赴", false));
                        
                    attributes.add(createAttribute(
                        "attribute.addicted", "成瘾",
                        "吃喝玩乐缺少节制", false));
                        
                    attributes.add(createAttribute(
                        "attribute.fantasy", "空想",
                        "做白日梦，不去行动", false));
                        
                    attributes.add(createAttribute(
                        "attribute.float", "虚浮",
                        "不接地气，过于理想化，不考虑实际情况", false));
                        
                    attributes.add(createAttribute(
                        "attribute.timid", "怯懦",
                        "因为害怕失败而拒绝挑战或错失机会", false));
                        
                    attributes.add(createAttribute(
                        "attribute.pretend", "矫情",
                        "口是心非、委曲求全、傲娇", false));
                        
                    attributes.add(createAttribute(
                        "attribute.arrogant", "傲慢",
                        "骄傲自大和看轻他人", false));
                        
                    attributes.add(createAttribute(
                        "attribute.impatient", "急躁",
                        "急于知道结果，缺少耐心", false));
                        
                    attributes.add(createAttribute(
                        "attribute.anger", "嗔怒",
                        "试图回击他人的恶意", false));
                        
                    attributes.add(createAttribute(
                        "attribute.unsocial", "孤僻",
                        "没有把握住交际机会", false));
                        
                    attributes.add(createAttribute(
                        "attribute.rash", "轻率",
                        "言多必失，把自己的秘密告诉他人", false));
                        
                    attributes.add(createAttribute(
                        "attribute.onhand", "独裁",
                        "拒绝转包工作，总想教别人做事", false));

                    gson.toJson(attributes, writer);
                }
            }
        } catch (IOException e) {
            Toast.makeText(context, "创建属性数据文件失败: " + e.getMessage(), 
                Toast.LENGTH_SHORT).show();
        }
    }

    private Attribute createAttribute(String id, String name, String description, boolean isPositive) {
        Attribute attribute = new Attribute();
        attribute.setId(id);
        attribute.setName(name);
        attribute.setDescription(description);
        attribute.setLevel(0);
        attribute.setExperience(0);
        attribute.setPositive(isPositive);
        return attribute;
    }
}